﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;

namespace Galaktika.BI.Addin.VisifireChart
{
    internal static class Resources
    {
        public const string PIVOT_CHART_16 = "Galaktika.BI.Addin.VisifireChart.Images.x16.PivotChart.png";

        #region Images
        public class Images
        {
            private const string ADDIN_IMAGE32 = "Galaktika.BI.Addin.VisifireChart.Images.Visifire.ico";
            public static Image Addin32
            {
                get
                {
                    return Resources.GetImage(ADDIN_IMAGE32);
                }
            }

            public static Image PivotChartView
            {
                get
                {
                    return Resources.GetImage("Galaktika.BI.Addin.VisifireChart.Images.PivotChartView.png");
                }
            }
        }
        #endregion
        #region Cache
        private class ResourceCache
        {
            private Dictionary<string, WeakReference> m_Cache;
            private ResourceCache()
            {
                m_Cache = new Dictionary<string, WeakReference>();
            }

            private static ResourceCache m_Inst;
            public static ResourceCache Instance
            {
                get
                {
                    if (m_Inst == null)
                    {
                        m_Inst = new ResourceCache();
                    }

                    return m_Inst;
                }
            }

            public Image this[Assembly asm, string id]
            {
                get
                {
                    WeakReference wr;
                    m_Cache.TryGetValue(id, out wr);
                    if (wr == null || !wr.IsAlive)
                    {
                        Stream stream = asm.GetManifestResourceStream(id);
                        if (stream == null)
                        {
                            return null;
                        }

                        WeakReference newWr = new WeakReference(Image.FromStream(stream));
                        if (wr == null)
                        {
                            m_Cache.Add(id, newWr);
                        }
                        else
                        {
                            m_Cache[id] = newWr;
                        }
                        wr = newWr;
                    }

                    return (Image)wr.Target;
                }
            }
        }
        #endregion

        public static Image GetImage(Type type, string resName)
        {
            return GetImage(type.Assembly, resName);
        }

        public static Image GetImage(Assembly asm, string resName)
        {
            return ResourceCache.Instance[asm, resName];
        }

        public static Image GetImage(string resName)
        {
            return GetImage(typeof(Resources), resName);
        }

        public static Icon GetIcon(string resName)
        {
            return GetIcon(typeof(Resources), resName);
        }

        public static Icon GetIcon(Type type, string resName)
        {
            Stream stream = type.Assembly.GetManifestResourceStream(resName);
            return new Icon(stream);
        }
    }
}
